# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-brieflz
_pkgname=brieflz.lua
pkgver=0.2.0
pkgrel=0
pkgdesc="Lua binding for BriefLZ compression library"
url="https://github.com/jirutka/brieflz.lua"
arch="all"
license="MIT"
checkdepends="lua-basexx lua-busted moonscript"
source="$pkgname-$pkgver.tar.gz::https://github.com/jirutka/$_pkgname/archive/v$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"

case "$CARCH" in
	aarch64) _luajit="";;
	*) checkdepends="$checkdepends luajit" _luajit="jit";;
esac

_luaversions="5.1 5.2 5.3"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	checkdepends="$checkdepends lua$_v-busted"
done

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
			CC="$CC" \
			CFLAGS="$CFLAGS -fPIC" \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			make --tree=./build brieflz-dev-0.rockspec
	done
}

check() {
	local lver; for lver in $_luaversions $_luajit; do
		msg "Testing on lua$lver"

		ln -sf build/lib/lua/${lver/jit/5.1}/brieflz.so .
		lua$lver /usr/bin/busted
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	cd "$builddir"
	install -D -m 755 build/lib/lua/$lver/brieflz.so \
		"$subpkgdir"/usr/lib/lua/$lver/brieflz.so
}

sha512sums="c0b6332c5a8bbc2dda6596573fa995bf690c748586934a4e23da0660e7d6d4521b5e709d2de0622bb018dbbfed44bec14eda312af97282fb046d892f3e5a35b5  lua-brieflz-0.2.0.tar.gz"
